/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.school.service;

import java.util.List;

import com.jeesite.modules.file.entity.FileEntity;
import com.jeesite.modules.file.entity.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.school.entity.School;
import com.jeesite.modules.school.dao.SchoolDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * schoolService
 * @author cxy
 * @version 2019-04-10
 */
@Service
@Transactional(readOnly=true)
public class SchoolService extends CrudService<SchoolDao, School> {

	@Value("${imageBaseUrl}")
	private String imageBaseUrl;
	
	/**
	 * 获取单条数据
	 * @param school
	 * @return
	 */
	@Override
	public School get(School school) {
		return super.get(school);
	}
	
	/**
	 * 查询分页数据
	 * @param school 查询条件
	 * @param
	 * @return  page 分页对象
	 */
	@Override
	public Page<School> findPage(School school) {
		Page<School> page = super.findPage(school);
		List<School> list = page.getList();
		for (School entity : list) {
			List<FileUpload> pictureUrl = FileUploadUtils.findFileUpload(entity.getId(), "pictureUrl");
			if (pictureUrl != null && pictureUrl.size() > 0) {
				FileEntity fileEntity = pictureUrl.get(0).getFileEntity();
				entity.setPictureUrl(imageBaseUrl + fileEntity.getFilePath() + fileEntity.getFileId() + "." + fileEntity.getFileExtension());
			}
		}
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param school
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(School school) {
		super.save(school);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(school.getId(), "pictureUrl");
		FileUploadUtils.saveFileUpload(school.getId(), "qualification");
		FileUploadUtils.saveFileUpload(school.getId(), "healthCard");
		FileUploadUtils.saveFileUpload(school.getId(), "manager");
		FileUploadUtils.saveFileUpload(school.getId(), "promise");
		FileUploadUtils.saveFileUpload(school.getId(), "source");
	}
	
	/**
	 * 更新状态
	 * @param school
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(School school) {
		super.updateStatus(school);
	}
	
	/**
	 * 删除数据
	 * @param school
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(School school) {
		super.delete(school);
	}
	
}